package org.management.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

/**
 * 加密类
 */
public class EncryptUtils {

    /**
     * 加密方式
     */
    private static final String ENCRYPT_METHOD = "MD5";

    /**
     * 加密次数
     */
    private static final int ENCRYPT_NUMBER = 2;

    private static final String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6C8mTClbRsvQB+urYp9EO3fXU6pFzG/f4yG5+jTYro/pP4FMwV/0/JqKvZ8VOrwCXlsAhdFe13gnogCf0agvhW2A7j1Q6/mnXspfD2ktJy1SqeMFqcEzzcIbjgxzEjYu2UXSB5Bvzft+JC/WUKKRZMinTG2PW+xVh82wLfCkElwIDAQAB";

    private static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALoLyZMKVtGy9AH66tin0Q7d9dTqkXMb9/jIbn6NNiuj+k/gUzBX/T8moq9nxU6vAJeWwCF0V7XeCeiAJ/RqC+FbYDuPVDr+adeyl8PaS0nLVKp4wWpwTPNwhuODHMSNi7ZRdIHkG/N+34kL9ZQopFkyKdMbY9b7FWHzbAt8KQSXAgMBAAECgYAt6uDaUK/y25cLshEqXejkIXYeEbLKVTnThix3QYUAKl2KP1Ro1x6i6nc2kYcKD3k28LcEysEnGuEZKCA0W8QIc31OyKJc0usMY4nU/jgn0yhkc5loA9HyPih650ax5FvrVzEgn79DAEEdFr2cQJILuJOE0B7sDYrfakA7bS7Z8QJBAPLENO6vGpAzM6pWXXX+Xp2eGWhQMGrMnATudL6wouPwJWGn9zHeRBB2+tmtRdRjt1ZSDNPNlhU3L7lA3RL6uDkCQQDEMAztc1wT4CRU4sM3OR5XikoIg+iSnVHM0w31QTTP7XWR/E9tMKXdGZLmOSDK0qWFUD4gZ1sNa6hbcUCGrYNPAkEAzZCC2dGABra0evoirbU03PZwwTmVpfmX6z0XshjT9Xr1zFl0Ic/5ho+ZHhVw2cWOCxPzNK4S9egDCzT3fgi7UQJAepXjngM1B55IVVoTzM+968kXFVcG3YW7dRZzZEnILBweGIflQT7ihNLT/VK48MLKdhB7oVezhyE741dSkFSe9QJBAJONYp4fO7+/R9nrx9wNKQvulw/qaIfMtFXCPJiSlNhVzYIQPhFB+mVlVMFo/jGao7P+ofeOr39oVVpjFYP1szM=";


    /**
     * 加盐
     * @param password 密码
     * @param salt 用户名
     * @return 加密密码
     */
    public static String  encryptMd5Password(String password, String salt) {
        return new SimpleHash(ENCRYPT_METHOD, password, ByteSource.Util.bytes(salt),
                ENCRYPT_NUMBER).toHex();
    }

    /**
     * 加盐
     * @param password 密码
     * @return 加密密码
     */
    public static String  encryptMd5Password(String password) {
        return new SimpleHash(ENCRYPT_METHOD, password, ENCRYPT_NUMBER).toHex();
    }

    /**
     * 获取公钥
     * @return String
     */
    public static String getPublicKey() {
        return PUBLIC_KEY;
    }

    /**
     * 获取私钥
     * @return String
     */
    public static String getPrivateKey() {
        return PRIVATE_KEY;
    }

    /**
     * 随机生成密钥对
     */
    private static void genKeyPair() {
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
            keyPairGen.initialize(1024,new SecureRandom());
            KeyPair keyPair = keyPairGen.generateKeyPair();
            RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
            RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
            String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
            System.out.println(publicKeyString);
            String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
            System.out.println(privateKeyString);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
    /**
     * RSA公钥加密
     *
     * @param str
     *        加密字符串
     * @return 密文
     *        加密过程中的异常信息
     */
    public static String encryptRsaPassword( String str){
        byte[] decoded = Base64.decodeBase64(PUBLIC_KEY);
        RSAPublicKey pubKey;
        try {
            pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * RSA私钥解密
     *
     * @param str
     *            加密字符串
     * @return 铭文
     *             解密过程中的异常信息
     */
    public static String decryptRsaPassword(String str) {
        try {
            //64位解码加密后的字符串
            byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
            //base64编码的私钥
            byte[] decoded = Base64.decodeBase64(PRIVATE_KEY);
            RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
            //RSA解密
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, priKey);
            return new String(cipher.doFinal(inputByte));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        genKeyPair();
    }
}
